// layui.use(["layer", "laytpl", "element", "form", "slider", "laydate", "rate", "colorpicker", "layedit", "carousel", "upload"],
//     function() {
//         var $ = layui.jquery,
//             layer = layui.layer,
//             laytpl = layui.laytpl,
//             setter = layui.cache,
//             element = layui.element,
//             slider = layui.slider,
//             laydate = layui.laydate,
//             rate = layui.rate,
//             colorpicker = layui.colorpicker,
//             carousel = layui.carousel,
//             form = layui.form,
//             upload = layui.upload,
//             layedit = layui.layedit;
//         $(document).ready(function () {
//             layui.use('form', function () {
//
//                 form.render();   //表单渲染，得渲染才会有效果显示出来
//             })
//         })









 let provincesId=$("#provincesId").val();
 let citiesId=$("#citiesId").val();
 let areasId=$("#areasId").val();

 let  data={"studentId":$("#stuId").val()}

$.ajax({
    type:"post",
    url:'/updateView',
    data:JSON.stringify(data),
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success:function (stu) {
        $.ajax({
            type:"post",
            url:"/provinces",
            contentType: "application/json;charset=utf-8",
            dataType:"json",
            success:function (provinces) {
                var $province="<option value=''>请选择</option>";
                for(var i=0;i<provinces.length;i++) {
                    $province+="<option value="+provinces[i].provinceid+">"+provinces[i].province+"</option>"
                }
                $("select[name='province']").html($province);
                $("select[name='province']").val(provincesId);



                //回显原籍贯
                $.ajax({
                    type:"post",
                    url:"/cities",
                    contentType: "application/json;charset=utf-8",
                    dataType:"json",
                    success:function (cities) {
                        var $city="<option value=''>请选择</option>";
                        for(var i=0;i<cities.length;i++) {
                            if($("select[name='province'] option:selected").val()==cities[i].provinceid){
                                $city+="<option value="+cities[i].cityid+">"+cities[i].city+"</option>"
                            }
                        }
                        $("select[name='city']").html($city);
                        $("select[name='city']").val(citiesId);


                        $.ajax({
                            type:"post",
                            url:"/areas",
                            contentType: "application/json;charset=utf-8",
                            dataType:"json",
                            success:function (areas) {
                                var $area="<option value=''>请选择</option>"
                                for(var i=0;i<areas.length;i++) {
                                    if($("select[name='city'] option:selected").val()==areas[i].cityid){
                                        $area+="<option value="+areas[i].areaid+">"+areas[i].area+"</option>"
                                    }
                                }
                                $("select[name='area']").html($area);
                                $("select[name='area']").val(areasId);

                            },error:function () {
                                layer.msg("请求失败4");
                            }
                        })

                    },error:function () {
                        layer.msg("请求失败3");
                    }
                })
            },error:function (error) {
                layer.msg("请求失败1");
            }
        })



                //当select发生改变时
                $("select[name='province']").change(function(){
                    $.ajax({
                        type:"post",
                        url:"/cities",
                        contentType: "application/json;charset=utf-8",
                        dataType:"json",
                        success:function (cities) {
                            var $city="<option value=''>请选择</option>";
                            for(var i=0;i<cities.length;i++) {
                                if($("select[name='province'] option:selected").val()==cities[i].provinceid){
                                    $city+="<option value="+cities[i].cityid+">"+cities[i].city+"</option>"
                                }
                            }
                            $("select[name='city']").html($city);
                            form.render('select');
                        },error:function () {
                            layer.msg("改变请求失败3");
                        }
                    })
                });


                            $("select[name='city']").change(function(){
                                $.ajax({
                                    type:"post",
                                    url:"/areas",
                                    contentType: "application/json;charset=utf-8",
                                    dataType:"json",
                                    success:function (areas) {
                                        var $area="<option value=''>请选择</option>"
                                        for(var i=0;i<areas.length;i++) {
                                            if($("select[name='city'] option:selected").val()==areas[i].cityid){
                                                $area+="<option value="+areas[i].areaid+">"+areas[i].area+"</option>"
                                            }
                                        }
                                        $("select[name='area']").html($area);
                                        form.render();
                                    },error:function () {
                                        layer.msg("改变请求失败4");
                                    }
                                })
                            })


            },error:function (error) {
                layer.msg("请求失败2");
            }
        })




 // $("#addstu").click(function (){
 //     layer.open({
 //         type: 1 //此处以iframe举例
 //         , title:'我的宿舍'
 //         , area: ['800px', '650px']
 //         , shade: 0
 //         , maxmin: true
 //         ,content:$("#banjistu")
 //     })
 //
 //
 // });
 $("#addstu").click(function () {
     let gradeId=$("#gradeId").val()
     layer.open({
         type: 2 //此处以iframe举例
         , area: ['1300px', '700px']
         , shade: 0
         , maxmin: true
         ,content:"/selectbanjistu/"+gradeId

     })

 })

        $("#gsAll").click(function () {
            let dormitoryId=$("#dormitoryId").val()
            layer.open({
                type: 2 //此处以iframe举例
                , area: ['1300px', '700px']
                , shade: 0
                , maxmin: true
                ,content:"/selectsushestu/"+dormitoryId

            })

        })

//输入框获得焦点
$("input").focus(function(){
    $("input").css("background-color","#FFFFCC");
});

//失去焦点
$("input").blur(function(){
    $("input").css("background-color","#fff");


    var updData={
        "studentId":$("#stuId").val(),
        "studentName":$("#stuName").val(),
        "mobile":$("#mobile").val(),
        "sex":$("#sex").val(),
        "idType":$("#idTypes").val(),
        "idNum":$("#IdNum").val(),
        "provincesId":$("select[name='province']").val(),
        "citiesId":$("select[name='city']").val(),
        "areasId":$("select[name='area']").val(),
    }
    $.ajax({
        type:"post",
        url:"/updateStu",
        data:JSON.stringify(updData),
        contentType: "application/json;charset=utf-8",
        dataType:"json",
        success:function (upddata) {
            if(upddata.code=='0000'){
                layer.msg(''+upddata.msg,{icon:6,time:1000},function () {
                    window.location.reload();
                })
            }else if(upddata.code=='9999'){
                layer.msg(''+upddata.msg,{icon:5,time:1000},function () {
                    window.location.reload();
                })
            }
        },error:function (error) {
            layer.msg("修改请求失败")
        }


    })

});








